package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;


    // 新增用户
    @PostMapping
    public int insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public int deleteUserById(@PathVariable int id) {
        return userService.deleteUserById(id);
    }

    // 修改用户
    @PutMapping
    public int updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    // 查询用户
    @GetMapping("/{id}")
    public User selectUserById(@PathVariable int id) {
        return userService.selectUserById(id);
    }

    // 查询所有用户
    @GetMapping
    public List<User> selectAllUsers() {
        return userService.selectAllUsers();
    }
}
